package me.goldze.mvvmhabit.binding.viewadapter.image;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import androidx.databinding.BindingAdapter;

import me.goldze.mvvmhabit.R;
import me.goldze.mvvmhabit.utils.GlideCircleTransform;
import me.goldze.mvvmhabit.utils.RoundedCornersTransform;

/**
 * Created by goldze on 2017/6/18.
 */
public final class ViewAdapter {
    @BindingAdapter(value = {"url", "leftTop","rightTop","leftBottom","rightBottom","radius","placeholderRes"}, requireAll = false)
    public static void setImageUri(ImageView imageView, String url,
                                   boolean leftTop,boolean rightTop, boolean leftBottom, boolean rightBottom,
                                   int radius,int placeholderRes) {
        if (!TextUtils.isEmpty(url)) {
            //使用Glide框架加载图片
            if(!leftTop&&!rightTop&&!leftBottom&&!rightBottom){
                Glide.with(imageView.getContext())
                        .load(url)
                        .apply(new RequestOptions().placeholder(placeholderRes))
                        .into(imageView);
            }else {
                RoundedCornersTransform transform=new RoundedCornersTransform(imageView.getContext(),radius);
                transform.setNeedCorner(leftTop,rightTop,leftBottom,rightBottom);
                RequestOptions options = new RequestOptions().transform(transform);
                Glide.with(imageView.getContext()).asBitmap().load(url).apply(options).into(imageView);
            }
        }
    }
    @BindingAdapter(value = {"headerUrl", "placeholderRes"}, requireAll = false)
    public static void setHeaderUri(ImageView imageView, String url, int placeholderRes) {
//        if (!TextUtils.isEmpty(url)||"null".equals(url)) {
            //使用Glide框架加载图片
            Glide.with(imageView.getContext()).load(url)
                    .apply(new RequestOptions()
//                            .skipMemoryCache(true) // 不使用内存缓存
//                            .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                            .placeholder(R.drawable.default_headicon_5)
                            .error(R.drawable.default_headicon_5)
                            .centerCrop()
                            .transform(new GlideCircleTransform(imageView.getContext(), 1, Color.parseColor("#f3f3f3"))))
                    .into(imageView);
//        }else {
//            imageView.setBackground(imageView.getContext().getResources().getDrawable(R.mipmap.default_header));
//        }
    }

    @BindingAdapter(value = {"drawable"}, requireAll = false)
    public static void setDrawable(View imageView, int url) {
        if(url!=0){
            imageView.setBackground(imageView.getContext().getResources().getDrawable(url));
        }else {
            imageView.setBackground(null);
        }
    }
    @BindingAdapter(value = {"bitmapResource"}, requireAll = false)
    public static void setBitmap(ImageView imageView, Bitmap bitmap) {
        if (null != bitmap) {
            imageView.setImageBitmap(bitmap);
        }
    }
}

